﻿@page "/fetchdata"
@using MessageBoxControl.Data
@using System.Collections.ObjectModel
@inject WeatherForecastService ForecastService

@using MessageBoxControl.Controls.MessageBox
@inject MessageBoxControl.Controls.MessageBox.MessageBoxService MessageBoxService

<TelerikGrid Data="@forecasts">
    <GridColumns>
        <GridColumn Field="Date" />
        <GridColumn Field="TemperatureC" />
        <GridColumn Field="TemperatureF" />
        <GridColumn Field="Summary" />
        <GridCommandColumn Width="300px">
            <GridCommandButton OnClick="@((args)=> DeleteRecord(args.Item as WeatherForecast))" Icon="delete"></GridCommandButton>
        </GridCommandColumn>
    </GridColumns>
</TelerikGrid>


@code {
    void DeleteRecord(WeatherForecast forecastToDelete)
    {
        MessageBoxService.Show("Confirmation", "Are you really sure you want to delete this record ?", MessageBoxButtonTypeEnum.YesNo, (result) =>
        {
            if (result == MessageBoxResultEnum.Yes)
            {
                //finish the action in the callback
                forecasts.Remove(forecasts.FirstOrDefault(f => f.Id == forecastToDelete.Id));
                StateHasChanged();
                //in a real grid app, you should also call the actual data service
            }
        });
    }

    ObservableCollection<WeatherForecast> forecasts;
    protected override async Task OnInitializedAsync()
    {
        List<WeatherForecast> tempData = await ForecastService.GetForecastAsync(DateTime.Now);
        forecasts = new ObservableCollection<WeatherForecast>(tempData);
    }
}
